package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.ChooseResultData;
import com.jzg.jcptadmin.viewinterface.ChooseResultInterface;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/1/6.
 */
public class ChooseResultPresenter extends BasePresenter<ChooseResultInterface> {

    private DataManager dataManager;
    private ChooseResultInterface mView;
    private Subscription mSubscription;

    public ChooseResultPresenter(DataManager dataManager) {
        super();
        this.dataManager = dataManager;
    }

    @Override
    public void attachView(ChooseResultInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;

    }
    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    /**
     * 加载车源机构
     */
    public void LoadSourceData(){
        checkViewAttached();
        if(!AppContext.isNetWork){
            mView.showError(Constant.errr_net);
            return;
        }
        mSubscription = dataManager.loadChooseResult(mView.getParams())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new Observer<ChooseResultData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if(e!=null&& mView!=null){

                    mView.showError(e.toString());
                }
            }

            @Override
            public void onNext(ChooseResultData chooseResultData) {
                    mView.showDatas(chooseResultData);

            }
        });
    }


}
